home *** CD-ROM | disk | FTP | other *** search
/ Die Ultimative Software-P…i Collection 1996 & 1997 / Die Ultimative Software-Pakete CD-ROM fur Atari Collection 1996 & 1997.iso / g / gnu_c / pmllib21.zoo / tan.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-11  |  4.8 KB  |  248 lines

  1. /************************************************************************
  2.  *                                    *
  3.  *                N O T I C E                *
  4.  *                                    *
  5.  *            Copyright Abandoned, 1987, Fred Fish        *
  6.  *                                    *
  7.  *    This previously copyrighted work has been placed into the    *
  8.  *    public domain by the author (Fred Fish) and may be freely used    *
  9.  *    for any purpose, private or commercial.  I would appreciate    *
  10.  *    it, as a courtesy, if this notice is left in all copies and    *
  11.  *    derivative works.  Thank you, and enjoy...            *
  12.  *                                    *
  13.  *    The author makes no warranty of any kind with respect to this    *
  14.  *    product and explicitly disclaims any implied warranties of    *
  15.  *    merchantability or fitness for any particular purpose.        *
  16.  *                                    *
  17.  ************************************************************************
  18.  */
  19.  
  20.  
  21. /*
  22.  *  FUNCTION
  23.  *
  24.  *    tan   Double precision tangent
  25.  *
  26.  *  KEY WORDS
  27.  *
  28.  *    tan
  29.  *    machine independent routines
  30.  *    trigonometric functions
  31.  *    math libraries
  32.  *
  33.  *  DESCRIPTION
  34.  *
  35.  *    Returns tangent of double precision floating point number.
  36.  *
  37.  *  USAGE
  38.  *
  39.  *    double tan (x)
  40.  *    double x;
  41.  *
  42.  *  INTERNALS
  43.  *
  44.  *    Computes the tangent from tan(x) = sin(x) / cos(x).
  45.  *
  46.  *    If cos(x) = 0 and sin(x) >= 0, then returns largest
  47.  *    floating point number on host machine.
  48.  *
  49.  *    If cos(x) = 0 and sin(x) < 0, then returns smallest
  50.  *    floating point number on host machine.
  51.  *
  52.  *  REFERENCES
  53.  *
  54.  *    Fortran IV plus user's guide, Digital Equipment Corp. pp. B-8
  55.  *
  56.  */
  57.  
  58. #if !defined (__M68881__) && !defined (sfp004)    /* mjr++        */
  59.  
  60. #include <stdio.h>
  61. #include <math.h>
  62. #include "pml.h"
  63.  
  64. static char funcname[] = "tan";
  65.  
  66. double tan (x)
  67. double x;
  68. {
  69.     double sinx;
  70.     double cosx;
  71.     struct exception xcpt;
  72.  
  73.     sinx = sin (x);
  74.     cosx = cos (x);
  75.     if (cosx == 0.0) {
  76.     xcpt.type = OVERFLOW;
  77.     xcpt.name = funcname;
  78.     xcpt.arg1 = x;
  79.     if (!matherr (&xcpt)) {
  80.         fprintf (stderr, "%s: OVERFLOW error\n", funcname);
  81.         errno = ERANGE;
  82.         if (sinx >= 0.0) {
  83.         xcpt.retval = HUGE_VAL;
  84.         } else {
  85.         xcpt.retval = -HUGE_VAL;
  86.         }
  87.     }
  88.     } else {
  89.     xcpt.retval = sinx / cosx;
  90.     }
  91.     return (xcpt.retval);
  92. }
  93.  
  94. #endif    /* __M68881__ || sfp004    */
  95. #ifdef    sfp004
  96.  
  97. __asm("
  98.  
  99. comm =     -6
  100. resp =    -16
  101. zahl =      0
  102.  
  103. ");    /* end asm    */
  104.  
  105. #endif    sfp004
  106. #if defined (__M68881__) || defined (sfp004)
  107.  
  108.     __asm(".text; .even");
  109.  
  110. # ifdef    ERROR_CHECK
  111.  
  112.     __asm("
  113.  
  114. _Overflow:
  115.     .ascii \"OVERFLOW\\0\"
  116. _Domain:
  117.     .ascii \"DOMAIN\\0\"
  118. _Error_String:
  119.     .ascii \"tan: %s error\\n\\0\"
  120. .even
  121. | pml compatible tangent
  122. | m.ritzert 7.12.1991
  123. | ritzert@dfg.dbp.de
  124. |
  125. |    /* NAN  = {7fffffff,ffffffff}        */
  126. |    /* +Inf = {7ff00000,00000000}        */
  127. |    /* -Inf = {fff00000,00000000}        */
  128. |    /* MAX_D= {7fee42d1,30773b76}        */
  129. |    /* MIN_D= {ffee42d1,30773b76}        */
  130.  
  131. .even
  132. double_max:
  133.     .long    0x7fee42d1
  134.     .long    0x30273b76
  135. double_min:
  136.     .long    0xffee42d1
  137.     .long    0x30273b76
  138. NaN:
  139.     .long    0x7fffffff
  140.     .long    0xffffffff
  141. p_Inf:
  142.     .long    0x7ff00000
  143.     .long    0x00000000
  144. m_Inf:
  145.     .long    0xfff00000
  146.     .long    0x00000000
  147. ");
  148. # endif    ERROR_CHECK
  149.  
  150.     __asm(".even
  151. .globl _tan
  152. _tan:
  153.     ");    /* end asm    */
  154.  
  155. #endif    /* __M68881__ || sfp004    */
  156. #ifdef    __M68881__
  157.  
  158.     __asm("
  159.     ftand    a7@(4), fp0    | tan
  160.     fmoved    fp0,a7@-    | push result
  161.     moveml    a7@+,d0-d1    | return_value
  162.     ");    /* end asm    */
  163.  
  164. #endif    __M68881__
  165. #ifdef    sfp004
  166.     __asm("
  167.     lea    0xfffa50,a0
  168.     movew    #0x540f,a0@(comm)    | specify function
  169.     cmpiw    #0x8900,a0@(resp)    | check
  170.     movel    a7@(4),a0@        | load arg_hi
  171.     movel    a7@(8),a0@        | load arg_low
  172.     movew    #0x7400,a0@(comm)    | result to d0
  173.     .long    0x0c688900, 0xfff067f8    | wait
  174.     movel    a0@,d0
  175.     movel    a0@,d1
  176.     ");    /* end asm    */
  177.  
  178. #endif    sfp004
  179. #if defined (__M68881__) || defined (sfp004)
  180. # ifdef    ERROR_CHECK
  181.     __asm("
  182.     lea    double_max,a0    |
  183.     swap    d0        | exponent into lower word
  184.     cmpw    a0@(16),d0    | == NaN ?
  185.     beq    error_nan    |
  186.     cmpw    a0@(24),d0    | == + Infinity ?
  187.     beq    error_plus    |
  188.     cmpw    a0@(32),d0    | == - Infinity ?
  189.     beq    error_minus    |
  190.     swap    d0        | result ok,
  191.     rts            | restore d0
  192. ");
  193. #ifndef    __MSHORT__
  194. __asm("
  195. error_minus:
  196.     swap    d0
  197.     moveml    d0-d1,a7@-
  198.     movel    #63,_errno    | errno = ERANGE
  199.     pea    _Overflow    | for printf
  200.     bra    error_exit    |
  201. error_plus:
  202.     swap    d0
  203.     moveml    d0-d1,a7@-
  204.     movel    #63,_errno    | NAN => errno = EDOM
  205.     pea    _Overflow    | for printf
  206.     bra    error_exit    |
  207. error_nan:
  208.     moveml    a0@(24),d0-d1    | result = +inf
  209.     moveml    d0-d1,a7@-
  210.     movel    #62,_errno    | NAN => errno = EDOM
  211.     pea    _Domain        | for printf
  212. ");
  213. #else    __MSHORT__
  214. __asm("
  215. error_minus:
  216.     swap    d0
  217.     moveml    d0-d1,a7@-
  218.     movew    #63,_errno    | errno = ERANGE
  219.     pea    _Overflow    | for printf
  220.     bra    error_exit    |
  221. error_plus:
  222.     swap    d0
  223.     moveml    d0-d1,a7@-
  224.     movew    #63,_errno    | NAN => errno = EDOM
  225.     pea    _Overflow    | for printf
  226.     bra    error_exit    |
  227. error_nan:
  228.     moveml    a0@(24),d0-d1    | result = +inf
  229.     moveml    d0-d1,a7@-
  230.     movew    #62,_errno    | NAN => errno = EDOM
  231.     pea    _Domain        | for printf
  232. ");
  233. #endif    __MSHORT__
  234. __asm("
  235. error_exit:
  236.     pea    _Error_String    |
  237.     pea    __iob+52    |
  238.     jbsr    _fprintf    |
  239.     addl    #12,a7        |
  240.     moveml    a7@+,d0-d1
  241.     rts
  242.     ");
  243. # else    ERROR_CHECK
  244. __asm("rts");
  245. # endif    ERROR_CHECK
  246.  
  247. #endif /* __M68881__ || sfp004    */
  248.